package com.omni.monitor.plugin.threadpool;


import org.springframework.stereotype.Service;

import java.util.concurrent.*;

//@Service
public class ThreadPoolMonitorDemo {
    private ThreadPoolExecutor orderProcessExecutor = null;
    private final ThreadPoolMonitor threadPoolMonitor;

    public ThreadPoolMonitorDemo(ThreadPoolMonitor threadPoolMonitor) {
        ThreadPoolExecutor orderProcessExecutor1;
        this.threadPoolMonitor = threadPoolMonitor;

        // 创建线程池
        orderProcessExecutor1 = new ThreadPoolExecutor(
                1,                 // 核心线程数
                3,                 // 最大线程数
                60, TimeUnit.SECONDS, // 空闲线程存活时间
                new LinkedBlockingQueue<>(5), // 工作队列
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
        // 注册到监控系统
        orderProcessExecutor1 =  this.threadPoolMonitor.register("订单处理线程池", orderProcessExecutor1);
        this.orderProcessExecutor = orderProcessExecutor1;


        ThreadPoolExecutor orderProcessExecutor2;


        // 创建线程池
        orderProcessExecutor2 = new ThreadPoolExecutor(
                1,                 // 核心线程数
                3,                 // 最大线程数
                60, TimeUnit.SECONDS, // 空闲线程存活时间
                new LinkedBlockingQueue<>(5), // 工作队列
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
        // 注册到监控系统
        orderProcessExecutor1 =  this.threadPoolMonitor.register("支付处理线程池", orderProcessExecutor2);
    }



    public CompletableFuture<String> processOrderAsync() {
        return CompletableFuture.supplyAsync(() -> {
            // 处理订单逻辑
            return "123";
        }, orderProcessExecutor);
    }
}
